import {Component} from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';

import _ from 'lodash';
import moment from 'moment';

import {FtMenu} from '../../ft-comp/ft-menu';

import {conf} from '../../config';
import {Backend} from '../../providers/backend';

import {Files} from './tab-news-files';

@Component({
  templateUrl: 'build/pages/tab-news/tab-news.html',
  directives: [FtMenu],
})
export class TabNews {

  static get parameters() {
    return [[NavController], [NavParams], [Backend]];
  }

  constructor(nav, params, backend) {
    this.nav = nav;
    this.params = params;
    this.backend = backend;

    this.fuwu = 'http://www.gtigroup.org';
    this.did;
    this.pageIndex;
    this.pageIndex1;
    this.menus = [
      { 'id': '1', 'name': 'GTI Updates' },
      { 'id': '2', 'name': 'Industry News' }
    ];

    // this.start = 0;
    // this.select_id = 1;
    // this.backend.newsCats()
    //   .then(result => {
    //     this.menus = result;
    //     // _.head(this.menus).selected = true;
    //     this.select_menu = _.head(this.menus);
    //   });
    /**
     * 获取resources White Paper列表
     */
    // this.backend.resourcesWhitePaper(1, this.start)

    //   .then(result => {
    //     this.select_menu = 1;
    //     this.resourcesWhitePaper = result.subjects;
    //     console.log(result);
    //   });
  }

  menuChanged(item) {
    console.log(item.id);
    if(item.id == 1){
      this.did = 1;
      this.pageIndex = 1;
      this.select_menu = 'Update';
      this.backend.resourcesWhitePaper(this.select_menu,this.pageIndex)
      .then(result => {
        this.resourcesWhitePaper = result.result[0];
        console.log(result.pageIndex);
        this.pageIndex=result.pageIndex;
      });
    }else{
      this.did = 2;
      this.select_menu = 'Industry';
      this.pageIndex1 = 1;
      this.backend.resourcesWhitePaper(this.select_menu,this.pageIndex1)
      .then(result => {
        this.resourcesWhitePaper = result.result[0];
        console.log(result.pageIndex);
        this.pageIndex1=result.pageIndex;
      });
    } 
  }
  // menuChanged(item) {
  //   this.select_id = item.id;
  //   this.select_menu = item;
  //   console.log(this.select_id);
  //   this.backend.dsQuery({
  //     entity: 'gtig$Article',
  //     viewName: 'article-list',
  //     query: 'select * from gtig$Article',
  //     first: 0,
  //     max: conf.pageSize
  //   }).then((result) => {
  //     this.resourcesWhitePaper = result;
  //     console.log(this.resourcesWhitePaper);
  //   });
  // }


  /**
  * 下拉刷新
  */
  doRefresh(refresher) {
    if(this.did == 1){
      this.select_menu = 'Update';
      this.pageIndex = 1;
      this.backend.resourcesWhitePaper(this.select_menu,this.pageIndex)
      .then(result => {
        this.resourcesWhitePaper = result.result[0];
        console.log(result.pageIndex);
        this.pageIndex=result.pageIndex;
      });
    }else{
      this.select_menu = 'Industry';
      this.pageIndex1 = 1;
      this.backend.resourcesWhitePaper(this.select_menu,this.pageIndex1)
      .then(result => {
        this.resourcesWhitePaper = result.result[0];
        console.log(result.pageIndex);
        this.pageIndex1=result.pageIndex;
      });
    } 
    setTimeout(() => {
      console.log('Async operation has ended');
      refresher.complete();
    }, 500);
  }

  doInfinite(infiniteScroll) {
   
    if(this.did == 1){
      this.pageIndex += 1;
      this.select_menu = 'Update';
      this.backend.resourcesWhitePaper(this.select_menu,this.pageIndex)
      .then(result => {
        if (this.pageIndex <= result.pageTotal) {
          result.result[0].forEach((subject) => {
            this.resourcesWhitePaper.push(subject);
          });
        }
        
      });
    }else{
      this.pageIndex1 += 1;
      this.select_menu = 'Industry';
      this.backend.resourcesWhitePaper(this.select_menu,this.pageIndex1)
      .then(result => {
        if (this.pageIndex1 <= result.pageTotal) {
          result.result[0].forEach((subject) => {
            this.resourcesWhitePaper.push(subject);
          });
        }
        
      });
    }
    
    setTimeout(() => {
      console.log('Async operation has ended');
      infiniteScroll.complete();
    }, 500);
  }
  toFile(id,classid) {
    
    this.nav.push(Files, { id: id ,classid:classid});
  }
}